package revolucion.window;

import revolucion.classes.User;
import revolucion.database.Database;

import java.awt.Dimension;

import javax.swing.JFrame;
import javax.swing.JTable;
import javax.swing.ImageIcon;
import javax.swing.table.DefaultTableModel;

public class Window_User extends JFrame {
  User target;
  User user;
  boolean edit;
  JTable table;
  DefaultTableModel tableModel;

  public Window_User( boolean _edit, User _target, User _user, JTable _table ) {
    initComponents();

    setIconImage(new ImageIcon(getClass().getResource("/revolucion/resources/admin.gif")).getImage());

    this.edit = _edit;
    this.user = _user;
    this.table = _table;

    if(this.edit) {
      this.setTitle(".:: Editar Usuário");

      this.target = _target;
      this.txt_user.setText(target.getUsername());
      this.txt_name.setText(target.getDisplayName());
      this.txt_email.setText(target.getEmail());
    } else {
      this.setTitle(".:: Novo Usuário");
    }

    Dimension dim = this.getToolkit().getScreenSize();
    int x = (int) (dim.getWidth()  - this.getSize().getWidth() ) / 2;
    int y = (int) (dim.getHeight() - this.getSize().getHeight()) / 2;
    this.setLocation(x,y);
  }

  @SuppressWarnings("unchecked")
  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  private void initComponents() {

    txt_name = new javax.swing.JTextField();
    txt_user = new javax.swing.JTextField();
    btn_changePass = new javax.swing.JButton();
    txt_email = new javax.swing.JTextField();
    jLabel1 = new javax.swing.JLabel();
    jLabel2 = new javax.swing.JLabel();
    jLabel3 = new javax.swing.JLabel();
    jLabel4 = new javax.swing.JLabel();
    jButton1 = new javax.swing.JButton();
    jButton2 = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
    setResizable(false);

    txt_name.setName("txt_name"); // NOI18N

    txt_user.setName("txt_user"); // NOI18N

    btn_changePass.setText("Mudar senha");
    btn_changePass.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
    btn_changePass.setName("btn_changePass"); // NOI18N

    txt_email.setName("txt_email"); // NOI18N

    jLabel1.setLabelFor(txt_user);
    jLabel1.setText("Usuário:");
    jLabel1.setName("jLabel1"); // NOI18N

    jLabel2.setLabelFor(txt_name);
    jLabel2.setText("Nome:");
    jLabel2.setName("jLabel2"); // NOI18N

    jLabel3.setLabelFor(btn_changePass);
    jLabel3.setText("Senha:");
    jLabel3.setName("jLabel3"); // NOI18N

    jLabel4.setLabelFor(txt_email);
    jLabel4.setText("Email:");
    jLabel4.setName("jLabel4"); // NOI18N

    jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/revolucion/resources/check.gif"))); // NOI18N
    jButton1.setToolTipText("Confirmar");
    jButton1.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
    jButton1.setName("jButton1"); // NOI18N
    jButton1.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        jButton1ActionPerformed(evt);
      }
    });

    jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/revolucion/resources/cancel.png"))); // NOI18N
    jButton2.setToolTipText("Cancelar");
    jButton2.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
    jButton2.setMaximumSize(new java.awt.Dimension(47, 23));
    jButton2.setMinimumSize(new java.awt.Dimension(47, 23));
    jButton2.setName("jButton2"); // NOI18N
    jButton2.setPreferredSize(new java.awt.Dimension(47, 23));
    jButton2.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        jButton2ActionPerformed(evt);
      }
    });

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(layout.createSequentialGroup()
        .addGap(39, 39, 39)
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
          .addComponent(jLabel1)
          .addComponent(jLabel2)
          .addComponent(jLabel3)
          .addComponent(jLabel4))
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
          .addGroup(layout.createSequentialGroup()
            .addComponent(btn_changePass)
            .addContainerGap())
          .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
              .addComponent(txt_email, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 217, Short.MAX_VALUE)
              .addComponent(txt_name, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 217, Short.MAX_VALUE)
              .addComponent(txt_user, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 217, Short.MAX_VALUE))
            .addGap(19, 19, 19))))
      .addGroup(layout.createSequentialGroup()
        .addGap(120, 120, 120)
        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
        .addContainerGap(140, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(layout.createSequentialGroup()
        .addGap(22, 22, 22)
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
          .addComponent(txt_user, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
          .addComponent(jLabel1))
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
          .addComponent(txt_name, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
          .addComponent(jLabel2))
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
          .addComponent(btn_changePass)
          .addComponent(jLabel3))
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
          .addComponent(txt_email, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
          .addComponent(jLabel4))
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
          .addComponent(jButton2, 0, 0, Short.MAX_VALUE)
          .addComponent(jButton1))
        .addContainerGap(14, Short.MAX_VALUE))
    );

    pack();
  }// </editor-fold>//GEN-END:initComponents

  private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
    this.dispose();
  }//GEN-LAST:event_jButton2ActionPerformed

  private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
    if(this.edit) {
      target.updateUser( user, target, this.txt_user.getText(), this.txt_name.getText(), this.txt_email.getText() );

      tableModel = Database.getUsersTableModel();
      table.setModel(tableModel);

      this.dispose();
    } else {
      User.saveUser( user, this.txt_user.getText(), this.txt_name.getText(), this.txt_email.getText());
      
      tableModel = Database.getUsersTableModel();
      table.setModel(tableModel);

      this.dispose();
    }
  }//GEN-LAST:event_jButton1ActionPerformed

  // Variables declaration - do not modify//GEN-BEGIN:variables
  private javax.swing.JButton btn_changePass;
  private javax.swing.JButton jButton1;
  private javax.swing.JButton jButton2;
  private javax.swing.JLabel jLabel1;
  private javax.swing.JLabel jLabel2;
  private javax.swing.JLabel jLabel3;
  private javax.swing.JLabel jLabel4;
  public javax.swing.JTextField txt_email;
  public javax.swing.JTextField txt_name;
  public javax.swing.JTextField txt_user;
  // End of variables declaration//GEN-END:variables

}
